﻿using Firefly.Framework.Input;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Input;

namespace Firefly.Framework.Windows.Forms
{
    public class Button : Control
    {
        protected string _style = "Default";

        public Button()
        {
        }

        protected override void OnMouseDown(MouseButtonMap map, MouseButtonMapEventArgs e)
        {
            _style = "Pressed";
            base.OnMouseDown(map, e);
        }

        protected override void OnMouseUp(MouseButtonMap map, MouseButtonMapEventArgs e)
        {
            _style = "Default";
            base.OnMouseUp(map, e);
        }

        protected override void OnMouseExit(MouseAxisMap map, MouseAxisMapEventArgs e)
        {
            _style = "Default";
            base.OnMouseExit(map, e);
        }

        protected override void OnKeyDown(KeyMap map, KeyMapEventArgs e)
        {
            if (HasFocus)
            {
                switch (map.MappedButtons[0])
                {
                    case Keys.Enter:
                        NotifyClick(new MouseButtonMapEventArgs());
                        break;
                }
            }

            base.OnKeyDown(map, e);
        }

        #region IPipelineSerializeable Members

        public override void DeserializeContent(ContentReader input)
        {
            base.DeserializeContent(input);
        }

        #endregion

        #region IDrawable Members

        public override void Draw(GameTime gameTime)
        {
            if (Visible)
            {
                if (this.HasFocus)
                {
                    WindowsService.Instance.Theme.Button.Draw("Focused", DrawRectangle, Color);
                }
                else
                {
                    WindowsService.Instance.Theme.Button.Draw(_style, DrawRectangle, Color);
                }

                Vector2 fontMeasurement = WindowsService.Instance.Theme.Fonts[Font].MeasureString(FontSize, Text);
                Rectangle textPosition = new Rectangle(DrawRectangle.X + (DrawRectangle.Width / 2) - ((int)fontMeasurement.X / 2),
                    DrawRectangle.Y + (DrawRectangle.Height / 2) - ((int)fontMeasurement.Y / 2),
                    (int)fontMeasurement.X,
                    (int)fontMeasurement.Y);
                WindowsService.Instance.Theme.Fonts[Font].Draw(Text, textPosition, FontSize, FontColor);
                DrawChildren(gameTime);
            }
        }

        #endregion
    }
}
